package com.flycms.module.comment.service;

import java.util.Date;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.flycms.module.comment.dao.QuestionsCountDao;
import com.flycms.module.comment.dao.QuestionsVoteDao;
import com.flycms.module.comment.model.FlyContentType;
import com.flycms.module.comment.model.OptionCount;
import com.flycms.module.comment.model.Votes;
import com.flycms.module.qasystem.dao.AnswersDao;
import com.flycms.module.qasystem.dao.QuestionsDao;

/**
 * 
 *
 * <br/>Copyright 2020
 * <br/>All rights reserved
 * @author 沁言訫語
 * @version 2020年3月8日
 * @Date 2020年3月8日
 */

@Service
public class OptionCountService {

    @Autowired
    protected QuestionsDao questionDao;
    
    @Autowired
    protected AnswersDao answerDao;
    
    @Autowired
    protected QuestionsVoteDao questionsVoteDao;
    
    @Autowired
    protected QuestionsCountDao questionsCountDao;
    
    @Transactional
    public int updateCount(OptionCount count, FlyContentType type) throws Exception {
        int i = 0;
        OptionCount o = null;
        switch (type) {
          case QUESTION:
              o = questionsCountDao.queryCountById(count.getId());
              if (o == null) {
                  questionsCountDao.addCount(count);
              } else {
                  o = setOptionCount(o, count);
                  questionsCountDao.updateCount(o);
              }
              break;
          default: break;
        }
        return i;
    }
    
    private static OptionCount setOptionCount(OptionCount o, OptionCount count) {
        o.setUpCount(getCount(o.getUpCount(), count.getUpCount()));
        o.setDownCount(getCount(o.getDownCount(), count.getDownCount()));
        o.setCollectCount(getCount(o.getCollectCount(), count.getCollectCount()));
        o.setCommentCount(getCount(o.getCommentCount(), count.getCommentCount()));
        return o;
    }
    
    private static int getCount(Integer c1, Integer c2) {
        c1 = c1 == null ? 0 : c1;
        c2 = c2 == null ? 0 : c2;
        int c = c1 + c2;
        return c >= 0 ? c : 0;
    }
    
    @Transactional
    public int updateVote(Long id, Integer voteType, Long userId, Date updateTime,
            FlyContentType type) throws Exception {
        Votes vote = new Votes();
        vote.setId(id);
        vote.setUserId(userId);
        vote.setVoteType(voteType);
        vote.setUpdateTime(updateTime);
        
        int i = 0;
        switch (type) {
          case QUESTION:
              i = questionsVoteDao.updateVote(vote);
              break;
          default: break;
        }
        return i;
    }
    
    public Votes queryVotedVote(Long userId, Long contentId, FlyContentType type) throws Exception {
        Votes vote = null;
        switch (type) {
          case QUESTION:
              vote = questionsVoteDao.queryVotedVote(userId, contentId);
              break;
          default: break;
        }
        return vote;
    }
    
}
